<?php

use yii\helpers\Html;
use common\components\GridView;
use yii\helpers\Url;
use yii\widgets\Pjax;
use backend\models\system\AdminLog;
use kartik\editable\Editable;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\system\search\AdminLogSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

?>

<?php Pjax::begin(['id' => 'grid-pjax']); ?>
    <div class="admin-index">

        <?php echo $this->render('_search', ['model' => $searchModel]); ?>
        <?= GridView::widget([
            'id' => 'grid-id',
            'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
//        'showPageSummary' => true,
            'pjax' => true,
            'striped' => true,
            'hover' => true,
            'toolbar' => [
                '{export}',
                ['content' =>
                    Html::a('<i class ="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
                ],
                'options' => ['class' => 'btn-group-sm']
            ],
            'exportConfig' => [
                GridView::CSV => ['label' => 'CSV', 'icon' => 'floppy-open'],
            ],
            'panel' => [
                'heading' => '<i class="glyphicon glyphicon-th-list"></i>',
                'type' => 'default',
                'before' => Html::a('<i class="fa fa-trash-o"></i> 清空', 'javascript:void(0);', ['class' => 'btn btn-default btn-sm btn-clear']) . ' ' .
                    Html::a('<i class="glyphicon glyphicon-remove"></i> 删除', 'javascript:void(0);', ['class' => 'btn btn-danger btn-sm batch-delete']),
            ],
            'pager' => [
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
                'registerLinkTags' => true,
                'disableCurrentPageButton' => true,
                'searchPage' => true,
                'searchPageLabel' => '跳至',
                'pageCount' => true,
                'pageSize' => true,
            ],
            'columns' => [
                [
                    'class' => \common\core\CheckboxColumn::className(),
                    'name' => 'id',
                    'options' => ['width' => '20px;'],
                    'checkboxOptions' => function ($model, $key, $index, $column) {
                        return ['value' => $key, 'label' => '<span></span>', 'labelOptions' => ['class' => 'mt-checkbox mt-checkbox-outline', 'style' => 'padding-left:19px;']];
                    }
                ],
                [
                    'label' => 'ID',
                    'attribute' => 'id',
                    'options' => ['width' => '50px;']
                ],
                [
                    'attribute' => 'title'
                ],
                [
                    'header' => '用户',
                    'options' => ['width' => '100px;'],
                    'content' => function ($model, $key) {
                        $user = (new \backend\models\system\Admin())->findByUserId($model['uid']);
                        return $user['username'];
                    }
                ],
                [
                    'label' => '查询字符串',
                    'value' => 'querystring',
                ],
                [
                    'label' => 'IP',
                    'value' => 'ip',
                ],
                [
                    'label' => '时间',
                    'value' => 'created_at',
                    'format' => ['date', 'php:Y-m-d H:i'],
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'header' => '操作',
                    'template' => '{delete}',
                    'options' => ['width' => '200px;'],
                    'buttons' => [
                        'delete' => function ($url, $model, $key) {
                            $options = [
                                'id' => $key,
                                'class' => 'btn btn-xs red btn-delete'
                            ];
                            return Html::a('<i class="fa fa-times"></i> 删除', null, $options);

                        },
                    ],
                ],
            ],
        ]); ?>

    </div>

    <script language="JavaScript">
        <?php $this->beginBlock(Yii::$app->controller->route) ?>

        $(document).ready(function () {

            // 清空
            $(".btn-clear").on("click", function () {
                // 询问框
                layer.confirm('您确定要执行清空操作吗？', {
                    btn: ['确认', '取消'], //按钮
                    icon: 7,
                    title:'提示'
                }, function (index) {
                    // 表单提交
                    $.ajax({
                        url: '<?= Url::toRoute('clear') ?>',
                        type: 'post',
                        dataType: "json",
                        data: {
                            _csrf: "<?= Yii::$app->request->csrfToken ?>"
                        },
                        beforeSend: function () {
                            layer.msg('清空中...');
                        },
                        success: function (r) {
                            layer.close(layer.index);
                            if (r.status == 1) {
                                layer.msg(r.info, {icon: 1});
                                $.pjax.reload({container: "#grid-pjax"});
                            } else {
                                layer.alert(r.info, {'title': '提示', 'icon': 7});
                            }
                        },
                        error: function () {
                            alert('系统错误');
                            return false;
                        }
                    });
                }, function (index) {

                });
            });

            // 删除
            $(".btn-delete").on("click", function () {
                var id = $(this).attr('id');
                if (!id.length) {
                    layer.alert('此记录一次，请刷新重试', {'title': '提示', 'icon': 7});
                    return false;
                }

                // 询问框
                layer.confirm('您确定要执行删除操作吗？', {
                    btn: ['确认', '取消'],//按钮
                    'title': '提示',
                    'icon': 7
                }, function (index) {
                    // 提交删除
                    subDel(id);
                }, function (index) {

                });
            });

            // 批量删除
            $(".batch-delete").on("click", function () {
                var ids = $("#grid-id").yiiGridView("getSelectedRows");
                if (!ids.length) {
                    layer.alert('请选择至少一条记录', {'title': '提示', 'icon': 7});
                    return false;
                }

                // 询问框
                layer.confirm('您确定要执行删除操作吗？', {
                    btn: ['确认', '取消'], //按钮
                    'title': '提示',
                    'icon': 7
                }, function (index) {
                    //提交删除
                    subDel(ids);
                }, function (index) {

                });


            });

            /**
             * 提交删除
             * @param ids
             */
            function subDel(ids) {
                // 表单提交
                $.ajax({
                    url: '<?= Url::toRoute('delete') ?>' + '?sync=true&id=' + ids,
                    type: 'post',
                    dataType: "json",
                    data: {
                        _csrf: "<?= Yii::$app->request->csrfToken ?>"
                    },
                    beforeSend: function () {
                        layer.msg('删除中...');
                    },
                    success: function (r) {
                        layer.close(layer.index);
                        if (r.status == 1) {
                            layer.msg(r.info, {icon: 1});
                            $.pjax.reload({container: "#grid-pjax"});
                        } else {
                            layer.alert(r.info, {'title': '提示', 'icon': 7});
                        }
                    },
                    error: function () {
                        alert('系统错误');
                        return false;
                    }
                });
            }

        });

        jQuery(document).ready(function () {
            highlight_subnav('system/admin-log/index'); //子导航高亮
        });

        <?php
        $this->endBlock();
        $this->registerJs($this->blocks[Yii::$app->controller->route], \yii\web\View::POS_END);
        ?>
    </script>
<?php Pjax::end(); ?>